package com.wolf.app.mytodo.db.provider;
 

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.wolf.app.mytodo.db.column.MyBaseColumns;
import com.wolf.app.mytodo.db.helper.DatabaseHelper;

public abstract class BaseProvider<T extends MyBaseColumns>  extends ContentProvider{

	private static SQLiteDatabase mDb;
	private static DatabaseHelper mDbHelper = null; 
	private Context context;
	private T t;
	 
	
	
	public BaseProvider(Context ctx){
		this.t = getColumns();
		context = ctx;
		onCreate();
	}
	  
	public abstract T getColumns();

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		if(context == null){
			context = getContext();
		}
		  
		try {
			if(mDbHelper != null){
				mDbHelper.close();
			}
			if(mDb != null){
				mDb.close();
			}
			mDbHelper = new DatabaseHelper(); 
			mDbHelper.open(context);
			mDbHelper.createTables(t.getCreateTableSQL());
			mDb = mDbHelper.mDb; 
			
		} catch (Exception e) {
			e.printStackTrace();
			mDb.close();
			mDbHelper.close();
		} 
		return true;

	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		int rowCount = mDb.delete(t.getTableName(), selection, selectionArgs); 
        // Notify any listeners and return the deleted row count.
		context.getContentResolver().notifyChange(uri, null);
        return rowCount;

	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		switch (t.getUriMatcher().match(uri)) {
        case MyBaseColumns.ALL:
            return "vnd.android.cursor.dir/gamecenter"; // List of items.
        case MyBaseColumns.SPECIFIC_ID:
            return "vnd.android.cursor.item/gamecenter"; // Specific item.
        default:
            return null;
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		long rowId = -1;
	       rowId = mDb.insert(t.getTableName(), null, values);
	       Uri newUri = Uri.withAppendedPath(uri, ""+rowId); 
	       // Notify any listeners and return the URI of the new row.
	       context.getContentResolver().notifyChange(uri, null);
	       return newUri;

	}
	
	public Cursor queryLimit(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder, String limit) {
		SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();

		// Set the table we're querying.
		qBuilder.setTables(t.getTableName());

		// If the query ends in a specific record number, we're
		// being asked for a specific record, so set the

		// WHERE clause in our query.
		if ((t.getUriMatcher().match(uri)) == t.SPECIFIC_ID) {
			qBuilder.appendWhere(t._ID + "="
					+ uri.getLastPathSegment());
		}

		// Make the query.
		Cursor c = qBuilder.query(mDb, projection, selection, selectionArgs,
				null, null, sortOrder, limit);
		c.setNotificationUri(context.getContentResolver(), uri);
		return c;
	}

	

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		return queryLimit(uri, projection, selection, selectionArgs, sortOrder, null);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		int updateCount = mDb.update(t.getTableName(), values,
				selection, selectionArgs);

		// Notify any listeners and return the updated row count.
		context.getContentResolver().notifyChange(uri, null);
		return updateCount;

	}

}
